<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="sbName">
        <el-input
          v-model="queryParams.sbName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="设备厂家" prop="sbVender">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.sbVender"-->
      <!--          placeholder="请输入设备厂家"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
<!--      <el-form-item label="厂家电话" prop="venderPhone">-->
<!--        <el-input-->
<!--          v-model="queryParams.venderPhone"-->
<!--          placeholder="请输入厂家电话"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <!--      <el-form-item label="采购时间" prop="caigouTime">-->
      <!--        <el-date-picker clearable-->
      <!--                        v-model="queryParams.caigouTime"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="请选择采购时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="出厂日期" prop="chuchangTime">-->
      <!--        <el-date-picker clearable-->
      <!--                        v-model="queryParams.chuchangTime"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--                        placeholder="请选择出厂日期">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="设备金额" prop="sbAmount">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.sbAmount"-->
      <!--          placeholder="请输入设备金额"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="负责人" prop="principal">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.principal"-->
      <!--          placeholder="请输入设备负责人"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
<!--      <el-form-item label="设备类型" prop="sbType">-->
<!--        <el-input-->
<!--          v-model="queryParams.sbType"-->
<!--          placeholder="请输入设备类型"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="设备状态" prop="sbStatus">
        <el-select v-model="queryParams.sbStatus" placeholder="请选择设备状态" clearable
                   @keyup.enter.native="handleQuery">
          <el-option label="正常" value="1"></el-option>
          <el-option label="停用" value="2"></el-option>
          <el-option label="报废" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shebei:shebei:export']"
        >导出
        </el-button>

      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        &lt;!&ndash;        <el-button&ndash;&gt;-->
    <!--        &lt;!&ndash;          type="primary"&ndash;&gt;-->
    <!--        &lt;!&ndash;          plain&ndash;&gt;-->
    <!--        &lt;!&ndash;          icon="el-icon-plus"&ndash;&gt;-->
    <!--        &lt;!&ndash;          size="mini"&ndash;&gt;-->
    <!--        &lt;!&ndash;          @click="handleAdd"&ndash;&gt;-->
    <!--        &lt;!&ndash;          v-hasPermi="['shebei:shebei:add']"&ndash;&gt;-->
    <!--        &lt;!&ndash;        >新增&ndash;&gt;-->
    <!--        &lt;!&ndash;        </el-button>&ndash;&gt;-->
    <!--        &lt;!&ndash;      </el-col>&ndash;&gt;-->
    <!--        &lt;!&ndash;      <el-col :span="1.5">&ndash;&gt;-->
    <!--        &lt;!&ndash;        <el-button&ndash;&gt;-->
    <!--        &lt;!&ndash;          type="success"&ndash;&gt;-->
    <!--        &lt;!&ndash;          plain&ndash;&gt;-->
    <!--        &lt;!&ndash;          icon="el-icon-edit"&ndash;&gt;-->
    <!--        &lt;!&ndash;          size="mini"&ndash;&gt;-->
    <!--        &lt;!&ndash;          :disabled="single"&ndash;&gt;-->
    <!--        &lt;!&ndash;          @click="handleUpdate"&ndash;&gt;-->
    <!--        &lt;!&ndash;          v-hasPermi="['shebei:shebei:edit']"&ndash;&gt;-->
    <!--        &lt;!&ndash;        >修改&ndash;&gt;-->
    <!--        &lt;!&ndash;        </el-button>&ndash;&gt;-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        &lt;!&ndash;        <el-button&ndash;&gt;-->
    <!--        &lt;!&ndash;          type="danger"&ndash;&gt;-->
    <!--        &lt;!&ndash;          plain&ndash;&gt;-->
    <!--        &lt;!&ndash;          icon="el-icon-delete"&ndash;&gt;-->
    <!--        &lt;!&ndash;          size="mini"&ndash;&gt;-->
    <!--        &lt;!&ndash;          :disabled="multiple"&ndash;&gt;-->
    <!--        &lt;!&ndash;          @click="handleDelete"&ndash;&gt;-->
    <!--        &lt;!&ndash;          v-hasPermi="['shebei:shebei:remove']"&ndash;&gt;-->
    <!--        &lt;!&ndash;        >删除&ndash;&gt;-->
    <!--        &lt;!&ndash;        </el-button>&ndash;&gt;-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->

    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="shebeiList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <!--      <el-table-column label="设备id" align="center" prop="shebeiId"/>-->
      <el-table-column label="设备名称" align="center" prop="sbName" width="300" show-overflow-tooltip/>
      <!--      <el-table-column label="设备厂家" align="center" prop="sbVender"/>-->
      <el-table-column label="厂家电话" align="center" prop="venderPhone" width="180" show-overflow-tooltip/>
      <!--      <el-table-column label="采购时间" align="center" prop="caigouTime">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.caigouTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="出厂日期" align="center" prop="chuchangTime">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.chuchangTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="设备金额" align="center" prop="sbAmount"/>
      <el-table-column label="设备类型" align="center" prop="sbType"/>
      <el-table-column label="设备状态" align="center" prop="sbStatus">
        <template slot-scope="scope">
          <span> {{
              scope.row.sbStatus == 1 ? "正常" : scope.row.sbStatus == 2 ? "停用" : scope.row.sbStatus == 3 ? "报废" : ""
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备负责人" align="center" prop="principal"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleUpdate(scope.row)"-->
      <!--            v-hasPermi="['shebei:shebei:edit']"-->
      <!--          >修改-->
      <!--          </el-button>-->
      <!--          &lt;!&ndash;          <el-button&ndash;&gt;-->
      <!--          &lt;!&ndash;            size="mini"&ndash;&gt;-->
      <!--          &lt;!&ndash;            type="text"&ndash;&gt;-->
      <!--          &lt;!&ndash;            icon="el-icon-delete"&ndash;&gt;-->
      <!--          &lt;!&ndash;            @click="handleDelete(scope.row)"&ndash;&gt;-->
      <!--          &lt;!&ndash;            v-hasPermi="['shebei:shebei:remove']"&ndash;&gt;-->
      <!--          &lt;!&ndash;          >删除&ndash;&gt;-->
      <!--          &lt;!&ndash;          </el-button>&ndash;&gt;-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="goWeixiu(scope.row)"-->
      <!--          >维修-->
      <!--          </el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="goBaoyang(scope.row)"-->
      <!--          >保养-->
      <!--          </el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="sbName">
          <el-input v-model="form.sbName" placeholder="请输入设备名称"/>
        </el-form-item>
        <el-form-item label="设备厂家" prop="sbVender">
          <el-input v-model="form.sbVender" placeholder="请输入设备厂家"/>
        </el-form-item>
        <el-form-item label="厂家电话" prop="venderPhone">
          <el-input v-model="form.venderPhone" placeholder="请输入厂家电话"/>
        </el-form-item>
        <el-form-item label="采购时间" prop="caigouTime">
          <el-date-picker clearable
                          v-model="form.caigouTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择采购时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出厂日期" prop="chuchangTime">
          <el-date-picker clearable
                          v-model="form.chuchangTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择出厂日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备金额" prop="sbAmount">
          <el-input v-model="form.sbAmount" placeholder="请输入设备金额"/>
        </el-form-item>
        <el-form-item label="设备负责人" prop="principal">
          <el-input v-model="form.principal" placeholder="请输入设备负责人"/>
        </el-form-item>
        <el-form-item label="设备类型" prop="sbType">
          <el-input v-model="form.sbType" placeholder="请输入设备类型"/>
        </el-form-item>
        <el-form-item label="设备状态" prop="sbStatus">
          <el-select v-model="form.sbStatus" placeholder="请选择设备状态" clearable
                     @keyup.enter.native="handleQuery">
            <el-option label="正常" value="1"></el-option>
            <el-option label="停用" value="2"></el-option>
            <el-option label="报废" value="3"></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="设备状态" prop="sbStatus">-->
        <!--          <el-input v-model="form.sbStatus" placeholder="请输入设备状态"/>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listShebei, getShebei, delShebei, addShebei, updateShebei} from "@/api/shebei/shebei";

export default {
  name: "Shebei",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      shebeiList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        sbName: null,
        sbVender: null,
        venderPhone: null,
        caigouTime: null,
        chuchangTime: null,
        sbAmount: null,
        principal: null,
        sbType: null,
        sbStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      listShebei(this.queryParams).then(response => {
        this.shebeiList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        shebeiId: null,
        sbName: null,
        sbVender: null,
        venderPhone: null,
        caigouTime: null,
        chuchangTime: null,
        sbAmount: null,
        principal: null,
        sbType: null,
        sbStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.shebeiId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const shebeiId = row.shebeiId || this.ids
      getShebei(shebeiId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.shebeiId != null) {
            updateShebei(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShebei(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    // 跳转设备维修页面
    goWeixiu(row) {
      this.$router.push({path: '/shebei/weixiujilu', query: {shebeiId: row.shebeiId}});
    },
    // 跳转保养页面
    goBaoyang(row) {
      this.$router.push({path: '/shebei/baoyangjilu', query: {shebeiId: row.shebeiId}});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const shebeiIds = row.shebeiId || this.ids;
      this.$modal.confirm('是否确认删除设备编号为"' + shebeiIds + '"的数据项？').then(function () {
        return delShebei(shebeiIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shebei/shebei/export', {
        ...this.queryParams
      }, `shebei_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
